import sqlite3
from datetime import datetime

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


conn = sqlite3.connect('E:\sqlite\GooglePlayAppsLink.db')
cursor = conn.cursor()


def set_domain():
    cursor.execute('SELECT id,url FROM duns where url != "" ')
    results = cursor.fetchall()
    for res in results:
        split = str(res[1]).split('.')
        split_ = split[len(split) - 2] + "." + split[len(split) - 1]
        print(split_)
        cursor.execute(
            'UPDATE duns SET domain = ? WHERE id =  ?', (split_, str(res[0])))
        conn.commit()

# start=1400
# end=1700

# country ="India"
# country ="United States"
# country ="United Kingdom"
# country ="Vietnam"
# country ="Philippines"
# country ="Indonesia"
# country ="Egypt"
# country ="Pakistan"
country ="India"
limit = 100
offset = 0
def get_domain():
    # cursor.execute('SELECT id,domain FROM duns where id>=? and id<? and contacts_number != ""',(start,end))
    cursor.execute('SELECT id,domain,url FROM duns where is_domain_checked is null and  country= ? and contacts_number != "" limit ? offset ?', (country, limit, offset))
    results = cursor.fetchall()
    for res in results:
        res_ = str(res[1]).split("/")[0]
        print(res_)

        cursor.execute('UPDATE duns SET is_domain_checked = 1 WHERE id ='  +str(res[0]))
        conn.commit()
get_domain()

def get_duns(domain):
    str1 = "'";
    # cursor.execute('SELECT id,domain FROM duns where id>=? and id<? and contacts_number != ""',(start,end))
    cursor.execute("SELECT name,duns,url FROM duns where domain like " + str1+"%"+ domain +"%"+str1+ " collate nocase")
    results = cursor.fetchall()
    for res in results:
        res_ = str(res[1]).split("/")[0]
        print(res[0]+"|"+res[1]+"|"+res[2])

# s = 'www.arrangeit.cm'
# domain = s.split('.')
# domain_ = s[s.find('.')+1:]
# print(domain_)
# with open("item.txt", 'r', encoding='utf-8') as f:
#     for query_key in f:
#         # replace = query_key.rstrip().replace("\n", "").lower()+"@gmail.com"
#         replace = query_key.rstrip().replace("\n", "").lower()
#         get_duns(replace)

